#Replication for Figures

####################################################################################################################################

#Figure 1:

#Creating Three Shape Parameters (a.0,a.2, and a.4), an ideal point (pi.c), and sequence of outcomes (pi)
  a.0 <- .0001
  a.2 <- 2
  a.4 <- 4
  pi <- seq(-2,2,.01)
  pi.c <- 0

#Calculating the linex losses for each value of a.0, a.2, and a.4
  loss.0 <- -(exp(a*(pi-pi.c))-(a*(pi-pi.c))-1)/(a*a)
  loss.2 <- -(exp(a.2*(pi-pi.c))-(a.2*(pi-pi.c))-1)/(a.2*a.2)
  loss.4 <- -(exp(a.4*(pi-pi.c))-(a.4*(pi-pi.c))-1)/(a.4*a.4)

#Final Figure
  pdf(file="fig1.pdf",width=4,height=3)
  
  par(mar=c(1,3,3,1))
  plot(x=pi,y=loss.0,type='l',ylim=c(-2,0),
     ann=FALSE,col="grey60", cex.axis=.75,bty='n',xaxt='n',yaxt='n')
  mtext(side=3,text="Inflation Outcomes",line=1,cex=.75)
  mtext(side=2,text="Linex Losses",line=2,cex=.75)
  axis(side=2,tck=0,cex.axis=.75)
  lines(x=pi,y=loss.2,lty=5,lwd=1)
  lines(x=pi,y=loss.4,lty=3,lwd=1.5)
  points(0,0,pch=16,cex=.75)
  abline(h=0)
  text(0,0,labels=expression(pi[c]),pos=1)

  dev.off()

####################################################################################################################################


#Figure 2:

#Setting Paramters for the government's preferences to determine hypothetical inflation level without delegation
  phi <- .25
  yg <- .25
  b <- 4
  pig <- 2
  
#Expected and Current Inflation  
  s.hat <- seq(3.75,8.25,.001)
  s <- rep(5,length(s.hat))
  x <- s.hat-s

#Inflation Outcome, given these parameters
  inf <- (((2*pig) + (b*s.hat))/(2+s.hat))-(.5*b*s)+(s*((((2*pig) + (b*s.hat))/(4+(2*s.hat)))))

#Final Figure
  pdf("fig3.pdf",width=6,height=3)
  par(mar=c(3,5.5,1,1))
  
  plot(x,inf,col="white",yaxt='n',ylab="",cex.axis=.75,xlab="",ylim=c(1.85,2.15),xlim=c(-.3,.5))
    mtext(expression(paste(hat(sigma)[epsilon])-sigma[epsilon]),side=1,line=2)
  
  inf.y <- inf[1477:4501]
    polygon(x=c(x[1477],seq(x[1477],3.25,length=length(inf.y)),3.25),y=c(2.0625,inf.y,2.0625),
          col="lightgrey",density=45)
  inf.y2 <- inf[1251:1476]
    polygon(x=c(0,seq(0,x[1476],length.out=length(inf.y2)),x[1476],0),y=c(2,inf.y2,2.0625,2.0625),
          col="black",density=35,lty=2,angle=90)
  inf.y3 <- inf[251:1250]
    polygon(x=c(x[251],seq(x[251],x[1250],length.out=length(inf.y3)),x[1250],x[251]),y=c(inf[251],inf.y3,2,2),
          col="black",lty=3,density=45,angle=135)
  
  lines(x,inf,lwd=3,col="black")
    segments(-1,2,0,2,lwd=1)
    segments(-1,pig+(phi*yg),1,pig+(phi*yg),lwd=1)
    segments(0,0,0,1.97,col="grey60",lty=3)
    segments(0,1.98,0,2,col="grey60",lty=3)
    segments(x[1477], 0, x[1477],inf[1477],lty=3,col="grey60")
    points(0,2,pch=18,cex=1.4)
    lines(x,inf,lwd=3,col="black")
    
  
  text(.1,2.0,"Equilibrium Inflation Outcome",srt=21,cex=.85)
  text(-.175,1.87,"Ambiguous\n Delegation",cex=.8)
  text(.1,1.87,"Effective\n Delegation",cex=.8)
  text(.4,1.87,"Ineffective\n Delegation",cex=.8)
  
  axis(side=2,at=c(2,2.0625),labels=c(expression(paste(pi["g"])),expression(paste(pi["no delegation"]))),las=2)
  axis(side=2,tck=0,at=2.25,labels="Inflation\nOutcomes",las=2,cex.axis=.8)
  
  box(which="plot")
  dev.off()


####################################################################################################################################

#Figure 4:

#Loading Hungary Data
  dat <- read.csv("hungary-vote-data-final.csv")

#Getting Uncertainty Measures by Meeting
  unc.bymeeting <- aggregate(dat$vote.unc,list(dat$meetid),mean)
  unc.bymeeting <- cbind(unc.bymeeting$x,unc.bymeeting$Group.1)
  unc.bymeeting <- as.data.frame(unc.bymeeting)
  colnames(unc.bymeeting)<-c("unc","meetid") 

#Final Figure
  pdf(file="fig4.pdf",width=6,height=3)
  par(mfrow=c(1,2),mar=c(4,2,1,1))

  hist(unc.bymeeting$unc,breaks=30,main="",xlab="",xlim=c(4,9),ylim=c(0,20),col="grey60",cex.axis=.75)
  title(xlab="Density of Uncertainty",line=2.5)
  segments(4,0,9,0)
  box(which="plot")

  par(mar=c(4,1,1,2))
  plot(unc.bymeeting$meetid,unc.bymeeting$unc,type='l',xlab="",xaxt='n',yaxt='n',ylim=c(4,9),ylab="")
  axis(side=1, at=c(2,60,120), labels=c(2005,2010,2015), cex.axis=.75)
  axis(side=4,cex.axis=.75)
  title(xlab="Uncertainty over Time",line=2.5)

  dev.off()

####################################################################################################################################

#Figure 5:  

#Loading Preference Estimates Data
  dat.pref <- read.csv("Hungary_Pref.csv")

#Sort Preferences Estimates Data by Fitted Values
  dat.pref <- dat.pref[order(dat.pref$cap.R),]
  mintomax <- seq(1:length(dat.pref$cap.R))
  dat.pref <- cbind(dat.pref,mintomax)

  par(mar=c(3,1,1,1))
  plot(x=dat.pref$cap.R,y=rep(1.25,27),col="white",ylim=c(1,3),yaxt='n')
  points(x=dat.pref$cap.R,y=rep(1.25,27),pch=16)
  text(x=dat.pref$cap.R,y=rep(1.25,27), labels = dat.pref$names,pos=4,srt=90)

#Visualization Correction for Overlapping Names 
  dat.pref$names
  plot.names <- c("G. Bager", "M. Nagy", "A. Balog", "G. Matolcsy", "C. Kandracs", "L. Windisch", "G. Pleschinger", "T. Banfi",
                "J. Cinkotai, G. Kocziszky", "", "F. Gerhardt", "A. Bartfai Mager", "V. Bihari", "G. Oblath",
                "B. Kadar", "I. Hardy", "C. Csaki", "J. Nemenyi, G. Szapary", "", "G. Kopits", "P. Adamecz", 
                "Z. Jarai", "H. Auth", "P. Bihari", "F. Karvalits", "A. Simor", "J. Kiraly")

#Final Figure
  pdf(file="fig5.pdf",width=16,height=4)
  par(mar=c(4,.5,.5,.5))
  plot(x=dat.pref$cap.R,y=rep(2,length(dat.pref$cap.R)),col="white",ylim=c(.5,3.5),yaxt='n',
       xlab=expression('R'[i]),ylab="")
  polygon(x=c( dat.pref$cap.R[4]-((dat.pref$cap.R[22]-dat.pref$cap.R[21])/2),
               dat.pref$cap.R[4]-((dat.pref$cap.R[22]-dat.pref$cap.R[21])/2),
               dat.pref$cap.R[4]+((dat.pref$cap.R[22]-dat.pref$cap.R[21])/2),
               dat.pref$cap.R[4]+((dat.pref$cap.R[22]-dat.pref$cap.R[21])/2)),
          y=c(0,4,4,0),col="lightgrey",border=NA)
  polygon(x=c(dat.pref$cap.R[22]-((dat.pref$cap.R[22]-dat.pref$cap.R[21])/2),
              dat.pref$cap.R[22]-((dat.pref$cap.R[22]-dat.pref$cap.R[21])/2), 
              dat.pref$cap.R[22]+((dat.pref$cap.R[22]-dat.pref$cap.R[21])/2), 
              dat.pref$cap.R[22]+((dat.pref$cap.R[22]-dat.pref$cap.R[21])/2)),
          y=c(0,4,4,0),col="lightgrey",border=NA)
  text(x=dat.pref$cap.R,y=rep(2,27), labels = plot.names,srt=90,cex=1.5)
  box(which="plot")
  abline(v=mean(dat.pref$cap.R),lty=2)
  dev.off()

####################################################################################################################################

  